package com.liush.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liush.common.helper.MenuHelper;
import com.liush.common.helper.RouterHelper;
import com.liush.model.controller.SysMenu;
import com.liush.model.controller.SysUser;
import com.liush.model.vo.RouterVo;
import com.liush.model.vo.SysUserQueryVo;
import com.liush.system.mapper.SysMenuMapper;
import com.liush.system.mapper.SysRoleMenuMapper;
import com.liush.system.mapper.SysUserMapper;
import com.liush.system.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author liush
 * @since 2023-02-25
 */
@Transactional
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public Page<SysUser> selectPageUser(Page<SysUser> page, SysUserQueryVo sysUserQueryVo) {
        return sysUserMapper.selectPageUser(page, sysUserQueryVo);
    }

    @Override
    public void updateStatus(Long userId, Integer status) {
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setStatus(status);
        sysUserMapper.updateById(sysUser);
    }

    @Override
    public SysUser getByUserName(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username);
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public Map getUserInfoByUserId(Long userId) {
        // 根据用户id：获取所有用户信息
        SysUser sysUser = sysUserMapper.selectById(userId);
        // 根据用户id：获取用户拥有的权限(路由-》菜单)
        List<SysMenu> sysMenuList = getMenuByUserId(userId);
        // 将菜单集合封装成菜单树
        List<SysMenu> buildMenuTree = MenuHelper.buildTree(sysMenuList);
        // 将菜单树封装成路由树
        List<RouterVo> routers = RouterHelper.buildRouters(buildMenuTree);
        // 根绝用户id：获取用户拥有的按钮权限
        List<String> buttons = getUserPermsBtnsByUserId(userId);

        Map<String, Object> map = new HashMap();
        map.put("name", sysUser.getName());
        map.put("avatar", sysUser.getHeadUrl());
        map.put("roles", "[\"admin\"]");
        map.put("routers", routers);
        map.put("buttons", buttons);
        return map;
    }

    @Override
    public List<String> getUserPermsBtnsByUserId(Long userId) {
        List<String> buttonsList = new ArrayList<>();
        List<SysMenu> sysMenuList = getMenuByUserId(userId);
        sysMenuList.forEach(sysMenu -> {
            if (sysMenu.getType() == 2) {
                buttonsList.add(sysMenu.getPerms());
            }
        });
        return buttonsList;
    }

    private List<SysMenu> getMenuByUserId(Long userId) {
        List<SysMenu> sysMenuList = null;
        if (userId.longValue() == 1) {
            sysMenuList = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getStatus, 1));
        } else {
            sysMenuList = sysMenuMapper.getMenuListByUserId(userId);
        }
        return sysMenuList;
    }
}
